package com.example.translateappjava.Activity.HomeFragmentActivity.Forum;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.translateappjava.R;
import com.example.translateappjava.Utils.Post;

public class ForumActivity extends AppCompatActivity implements ForumFragment.ForumListener {

    private ForumFragment forumFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forum);

        // 初始化论坛Fragment
        initForumFragment();
    }

    private void initForumFragment() {
        forumFragment = ForumFragment.newInstance();

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_container, forumFragment);
        transaction.commit();
    }

    @Override
    public void onPostClick(Post post) {
        // 处理帖子点击事件
//        Toast.makeText(this, "点击了帖子: " + post.getTitle(), Toast.LENGTH_SHORT).show();

        // 跳转到帖子详情页面
        Intent intent = new Intent(this, PostDetailActivity.class);
        intent.putExtra("post_title", post.getTitle());
        intent.putExtra("post_content", post.getContent());
        intent.putExtra("post_username", post.getUsername());
        intent.putExtra("post_time", post.getTime());
        intent.putExtra("post_category", post.getCategory());
        intent.putExtra("post_like_count", post.getLikeCount());
        intent.putExtra("post_comment_count", post.getCommentCount());
        startActivity(intent);
    }

    @Override
    public void onSearchClick() {
        // 处理搜索按钮点击
//        Toast.makeText(this, "打开搜索", Toast.LENGTH_SHORT).show();

        // 跳转到搜索页面
        Intent intent = new Intent(this, ForumSearchActivity.class);
        startActivity(intent);
    }

    @Override
    public void onAddPostClick() {
        // 处理发帖按钮点击
        Toast.makeText(this, "发布新帖", Toast.LENGTH_SHORT).show();

        // 跳转到发帖页面
        Intent intent = new Intent(this, CreatePostActivity.class);
        startActivityForResult(intent, 1001);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1001 && resultCode == RESULT_OK && data != null) {
            String title = data.getStringExtra("title");
            String content = data.getStringExtra("content");
            String category = data.getStringExtra("category");

            if (title != null && content != null && forumFragment != null) {
                // 生成一个临时的本地ID，或者后端成功保存后会返回真实ID
                String tempId = "local_" + System.currentTimeMillis();
                Post newPost = new Post(
                        tempId,
                        "我", // 实际应为当前登录用户名
                        title,
                        content,
                        category != null ? category : "经验分享", // 默认分类
                        "刚刚",
                        0,
                        0,
                        "https://example.com/my_avatar.jpg" // 当前用户头像
                );
                forumFragment.addPost(newPost);
                // TODO: (可选) 将 newPost 发送到后端保存
                Toast.makeText(this, "帖子发布成功", Toast.LENGTH_SHORT).show();
            }
        }
    }

    // 刷新论坛数据的公共方法
    public void refreshForumData() {
        if (forumFragment != null) {
            forumFragment.refreshData();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        // 返回到HomeFragment所在的Activity
        finish();
    }
}
